package com.skyvin.nreceipts.admin.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skyvin.nreceipts.admin.mongorepository.ReceiptCategoryTemplateRepository;
import com.skyvin.nreceipts.admin.service.ReceiptCategoryTemplateService;
import com.skyvin.nreceipts.common.document.ReceiptCategoryTemplate;
import com.skyvin.nreceipts.common.dto.ReceiptCategoryDTO;
import com.skyvin.nreceipts.common.mapper.ReceiptCategoryMapper;
import com.skyvin.nreceipts.common.value.JQGridResponse;
import com.skyvin.nreceipts.common.value.MessageValue;

@Controller
@RequestMapping(value="recCategoryTemplate")
public class ReceiptCategoryTemplateController {
	private static Logger logger = Logger.getLogger(ReceiptCategoryTemplateController.class);
	
	@Autowired
	private ReceiptCategoryTemplateRepository receiptCategoryTemplateRepository; 
	
	@Autowired
	private ReceiptCategoryTemplateService receiptCategoryTemplateService; 

	@RequestMapping(value="/loadRecCategoryPage", method=RequestMethod.GET)
	@PreAuthorize("hasAuthority('ADMIN')")
	public String loadReceiptCategory() {
		return "admin/receiptCategoryTemplateEdit";
	}
	
	@RequestMapping(value="/viewRecCategories", method=RequestMethod.GET)
	@ResponseBody
	public JQGridResponse viewReceiptCategories(@RequestParam("_search") Boolean search,
    		@RequestParam(value="filters", required=false) String filters,
    		@RequestParam(value="page", required=false) Integer page,
    		@RequestParam(value="rows", required=false) Integer rows,
    		@RequestParam(value="sidx", required=false) String sidx,
    		@RequestParam(value="sord", required=false) String sord) {
		
		List<ReceiptCategoryDTO> receiptCategoryDTOList = null;
		Page<ReceiptCategoryTemplate> receiptCategoryPageList = null;
		
		JQGridResponse jqGridResponse = new JQGridResponse();
		Pageable pageRequest = new PageRequest(page-1, rows);
		
		receiptCategoryPageList = receiptCategoryTemplateRepository.findAll(pageRequest);
		receiptCategoryDTOList = ReceiptCategoryMapper.map(receiptCategoryPageList);
		
		jqGridResponse.setPage(1);
		
		if(receiptCategoryPageList!= null) {
			
			jqGridResponse.setRecords(receiptCategoryPageList.getTotalElements());
			jqGridResponse.setTotal(receiptCategoryPageList.getTotalPages());
			jqGridResponse.setPage(receiptCategoryPageList.getNumber()+1);
		}
		jqGridResponse.setRows(receiptCategoryDTOList);
		return jqGridResponse;
	}
	
	@RequestMapping(value="/searchCategories", method=RequestMethod.GET)
	@ResponseBody
	public List<ReceiptCategoryTemplate> searchCategories(@RequestParam(value="term", required=false) String searchTerm) {
		List<ReceiptCategoryTemplate> receiptCategories = receiptCategoryTemplateRepository.findByValueLike(searchTerm);
		return receiptCategories;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('ADMIN')")
	public @ResponseBody MessageValue save(ReceiptCategoryTemplate receiptCategory) {
		logger.info("Receipt category passed: " + receiptCategory.getId() + "-value :" + receiptCategory.getValue());
		MessageValue message = new MessageValue();
		
		if(receiptCategory.getId()!=null && receiptCategory.getId().equals("_empty")) {
			receiptCategory.setId(null);
		}
		receiptCategoryTemplateService.save(receiptCategory);
		

		message.setMessage("Receipt Category Template created");
		//message.setDetailedMessage("http://farm4.staticflickr.com/3510/3208351775_e06db18ee4.jpg");

		return message;
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@PreAuthorize("hasAuthority('ADMIN')")
	public @ResponseBody MessageValue deleteReceiptCateogryTemplate(@RequestParam("oper") String oper, @RequestParam("id") String receiptCategoryPID) {
		MessageValue message = new MessageValue();
		receiptCategoryTemplateService.delete(receiptCategoryPID);
		message.setMessage("Receipt Item Category  deleted");
		return message;
	}
}
